package net.obxlabs.tweetpad.behaviour;

import net.nexttext.*;
import net.nexttext.behaviour.*;
import net.nexttext.property.*;

public class Level extends AbstractAction {
	private int levelOffset;
	
	Action childAction;
	
	public Level(int offset, Action child) {
		levelOffset = offset;
		childAction = child;
	}
	
	public ActionResult behave(TextObject to) {
		/*NumberProperty currHeightProp = (NumberProperty)to.getProperty("CurrHeight");
		if (currHeightProp == null) {
			// set it to the default value
			to.init("CurrHeight", new NumberProperty(targetHeightOffset));
			return new ActionResult(false, false, false);
		} else {
			int currHeight = (int)currHeightProp.get();
			TextObjectGroup tog = to.getParent();
			for (int i=1; i < currHeight; i++) {
				tog = tog.getParent();
			}
			return childAction.behave(tog);
		}*/
		
		int realOffset = levelOffset;
		
		// check if the TextObject needs to descend
		NumberProperty descendProp = (NumberProperty)to.getProperty("Descend");
		if (descendProp != null) {
			System.out.print("Found Descend prop, setting level from "+realOffset);
			realOffset -= descendProp.get();
			System.out.println(" to "+realOffset);
		}
		
		TextObject otherTo = to;
		
		if (realOffset > 0) {
			// move up in the tree
			for (int i=0; i < realOffset; i++) {
				otherTo = otherTo.getParent();
			}
		} else if (realOffset < 0) {
			// move down in the tree
			TextObjectGroup tog;
			for (int i=0; i < realOffset; i++) {
				tog = (TextObjectGroup)otherTo;
				otherTo = tog.getLeftMostChild();
			}
		}
	
		return childAction.behave(otherTo);
	}
	
	public void setOffset(int val) { levelOffset = val; }	
	public void incOffset() { levelOffset++; }
	public void decOffset() { levelOffset--; }
}
